import qs from 'qs';
import {
    myHome
} from '../api';

export default {
    namespace: 'register',
    state: {
        userMsg: {
            id: null,
            loginAccount: null,
            location: null
        },
        data: []
    },
    effects: {
        * saveUser({
            payload: {
                id,
                name,
                companyId
            }
        }, {
            call,
            put
        }) {
            yield put({
                type: 'register',
                payload: {
                    id,
                    name,
                    companyId
                }
            });
        },
        * queryUser({
            payload: {
                mobile,
                verify_code,
                type,
                zhuce
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.register, {
                mobile,
                verify_code,
                type
            });
            const data = jsonResult.data;
            localStorage.setItem('userDetail', JSON.stringify(data));
            if (typeof zhuce === 'function') {
                zhuce(data);
            }
            yield put({
                type: 'loginSuccess',
                payload: {
                    data: jsonResult.data
                }
            });
        },
        * sendCode({
            payload: {
                mobile,
                type,
                getCode
            }
        }, {
            call,
            put
        }) {
            // console.log(mobile)
            const {
                jsonResult
            } = yield call(myHome.sendCode, {
                mobile,
                // type
            });
            const data = jsonResult;

            if (typeof getCode === 'function') {
                getCode(data);
            }
            yield put({
                type: 'loginSuccess',
                payload: {
                    data: jsonResult.data
                }
            });
        },
        * sendKCode({
            payload: {
                mobile,
                // type,
                getCode
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.sendKCode, {
                mobile,
                // type
            });
            const data = jsonResult;
            console.log(jsonResult);
            if (typeof getCode === 'function') {
                getCode(data);
            }
            yield put({
                type: 'loginSuccess',
                payload: {
                    data: jsonResult
                }
            });
        },
        * sendAdminCode({
            payload: {
                mobile,
                // type,
                getCode
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(myHome.sendAdminCode, {
                mobile,
                // type
            });
            const data = jsonResult;
            // console.log(jsonResult);
            if (typeof getCode === 'function') {
                getCode(data);
            }
            yield put({
                type: 'loginSuccess',
                payload: {
                    data: jsonResult
                }
            });
        }
    },

    reducers: {
        register(state, {
            payload
        }) {
            return {
                ...state,
                userMsg: {
                    ...state.data,
                    ...payload
                }
            };
        },
        loginSuccess(state, {
            payload
        }) {
            return {
                ...state,
                data: {
                    ...state.data,
                    ...payload
                }
            };
        }
    }
};